﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.User
{
    public partial class userSale : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSubMenu();
                BindGridView();

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkEditProfile.NavigateUrl = "~/User/editProfile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkChangePassword.NavigateUrl = "~/User/changePassword.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSettings.NavigateUrl = "~/User/editSettings.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/userSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/userPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        /// <summary>
        /// Binds the gvUserSales GridView.
        /// </summary>
        private void BindGridView()
        {
            gvUserSale.DataSource = dbProfile.GetUserSales(Guid.Parse(Request.QueryString["uid"]));
            gvUserSale.DataBind();
        }

        //Event fires when the GridView is DataBound
        protected void gvUserSale_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string title = ((HyperLink)e.Row.Cells[0].Controls[0]).Text;

                foreach (Label lbl in e.Row.Cells[3].Controls.OfType<Label>())
                {
                    lbl.Text = (Boolean.Parse(lbl.Text)) ? GetLocalResourceObject("StatusActive").ToString() : GetLocalResourceObject("StatusInActive").ToString();
                }

                foreach (Label lbl in e.Row.Cells[1].Controls.OfType<Label>())
                {
                    string culture = String.Empty;

                    if (HttpContext.Current.Session["MyCulture"] != null)
                        culture = HttpContext.Current.Session["MyCulture"].ToString();

                    Sale sale = (Sale)e.Row.DataItem;

                    if (culture.Equals("en-GB"))
                        lbl.Text = sale.Category.LocalizedCategoryName;
                    else
                        lbl.Text = sale.Category.CategoryName;
                }

                foreach (LinkButton button in e.Row.Cells[4].Controls.OfType<LinkButton>())
                {
                    if (button.CommandName == "Delete")
                    {
                        button.Text = GetLocalResourceObject("btnDeleteText").ToString();
                        button.Attributes["onclick"] = "if(!confirm('" + GetLocalResourceObject("ConfirmBoxText").ToString() + "\"" + title + "\"?')) { return false; };";
                    }

                    if (button.CommandName == "Edit")
                    {
                        button.Text = GetLocalResourceObject("btnEditText").ToString();
                    }
                }
            }
        }

        /// <summary>
        /// Deletes a sale, based on the saleId parameter
        /// </summary>
        /// <param name="saleId">Specifies the ID of the sale to be deleted.</param>
        private void DeleteRow(int saleId)
        {
            dbProfile.DeleteUserSale(saleId);
        }

        //Event fired when clicking the Delete button in the GridView
        protected void gvUserSale_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int saleID = 0;

            foreach (Label lbl in gvUserSale.Rows[e.RowIndex].Cells[5].Controls.OfType<Label>())
	        {
                if(lbl.ID == "lblSaleID")
                    saleID = Convert.ToInt32(lbl.Text);
	        }

            if (saleID != 0)
                DeleteRow(saleID);

            BindGridView();
        }

        //Event fired when clicking the Edit button in the GridView
        protected void gvUserSale_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int saleID = 0;

            foreach (Label lbl in gvUserSale.Rows[e.NewEditIndex].Cells[5].Controls.OfType<Label>())
            {
                if (lbl.ID == "lblSaleID")
                    saleID = Convert.ToInt32(lbl.Text);
            }

            if (saleID != 0)
                Response.Redirect("~/User/editSale.aspx?uid=" + Request.QueryString["uid"] + "&sid=" + saleID);
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            hyLinkChangePassword.Text = GetLocalResourceObject("hyLinkChangePasswordText").ToString();
            hyLinkEditProfile.Text = GetLocalResourceObject("hyLinkEditProfileText").ToString();
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();
            hyLinkSettings.Text = GetLocalResourceObject("hyLinkSettingsText").ToString();

            lblUserSaleHeader.Text = GetLocalResourceObject("lblUserSaleHeaderText").ToString();
            hyLinkCreateSale.Text = GetLocalResourceObject("hyLinkCreateSaleText").ToString();

            gvUserSale.Columns[0].HeaderText = GetLocalResourceObject("gvUserSaleTitleText").ToString();
            gvUserSale.Columns[1].HeaderText = GetLocalResourceObject("gvUserSaleCategoryText").ToString();
            gvUserSale.Columns[2].HeaderText = GetLocalResourceObject("gvUserSaleDateText").ToString();
            gvUserSale.Columns[3].HeaderText = GetLocalResourceObject("gvUserSaleStatusText").ToString();

            gvUserSale.DataBind();
        }
    }
}